package util;

import domain.User;
import mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyBatisTest {
    static SqlSession session = null;
    static UserMapper mapper = null;
    static {
        try {
            session = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("sqlMapConfig.xml")).openSession();
            mapper = (UserMapper)session.getMapper(UserMapper.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void context() throws Exception{
        System.out.println(session.getConnection());
    }
    @Test
    public void testParam(){
        User user = mapper.findByCondition("老王", "男");
        //System.out.println(user);
    }

    @Test
    public void testParamByMap(){
        Map<String,Object> map = new HashMap<>();
        map.put("id",41);
        map.put("address","北京");
        User user = mapper.findByMap(map);
        System.out.println(user);
    }

    @Test
    public void testFindByUserName(){
        List<User> users = mapper.findByUserName("老王'or'1=1");
        users.forEach(System.out::println);
    }



}
